package com.yuyue.springFramework.note17.example.dao;

import com.yuyue.springFramework.note17.examp2.Person;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Person的持久化类
 */
public class PersonDao implements IPersonDao{
    JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    //插入方法
    public int insertTable(Person person){
        return jdbcTemplate.update("insert into person(name, age, sex, id) values (?, ?, ?, ?)"
                ,person.getName(),person.getAge(),person.getSex(),person.getId());
    }

    @Override
    //更新方法
    public int updateTable(Person person) {
        return jdbcTemplate.update("update person set name = ?, age = ?, sex = ? where id = ?"
                ,person.getName(),person.getAge(),person.getSex(),person.getId());
    }

    @Override
    //删除方法
    public int deleteTable(Person person) {
        return jdbcTemplate.update("delete from person where id = ?"
                , person.getId());
    }

    @Override
    //查询单个对象方法
    public Person queryForObject(Person person) {
        //使用Spring提供的行映射器填充对象
        return jdbcTemplate.queryForObject("select * from person where id = ?",
                new BeanPropertyRowMapper<>(Person.class), person.getId());
    }
}
